package zip;

import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;
import org.junit.Test;

import java.io.File;
import java.util.ArrayList;

/**
 * Created by csmd on 2017/12/5.
 */
public class test {
    @Test
    public  void dsn( ){
        File zipFile = new File("D:\\zipFile\\123456.zip");
        //加密.
        Zip4jUtil zip4jUtil  = new Zip4jUtil();
        zip4jUtil.setSrcPath("D:\\zipFile\\123456.zip");
         zip4jUtil.setPassword("123");
        zip4jUtil.encrypt();


    }
    @Test
    public void test2(){


                try {
                    //创建zip文件
                    ZipFile zipFile = new ZipFile("D:\\zipFile\\33333.zip");
                    File file=new File("D:\\zipFile\\33333.zip");
                    //增加文件到zip中
                    ArrayList<File> filesToAdd = new ArrayList<File>();
                    if(new File("D:/ceshi/1111.jpg").exists()){
                        filesToAdd.add(new File("D:/ceshi/111.jpg"));
                    }
                    if(new File("D:/ceshi/2222.jpg").exists()){
                        filesToAdd.add(new File("D:/ceshi/2222.jpg"));
                    }
                     //初始化各类参数
                    ZipParameters parameters = new ZipParameters();
                    //设置压缩模式
                    parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
                    parameters.setCompressionLevel(Zip4jConstants.DEFLATE_LEVEL_NORMAL);
                    //设置加密标志
                    parameters.setEncryptFiles(true);
                    //设置aes加密
                    parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
                    parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
                    //设置密码
                    parameters.setPassword("123456");
                    if(file.exists()&&file.isFile()){
                        file.delete();
                    }
                    zipFile.addFiles(filesToAdd, parameters);
                }
                catch (ZipException e)
                {
                    e.printStackTrace();
                }

    }
}
